package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.LockMode;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.FriendlinkType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 友情链接类型
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/friendlinktype")
public class AfriendlinktypeController extends BaseController {

    // 友情链接类型列表
    @RequestMapping
    public String index() {
        List<FriendlinkType> friendlinktypes = factorySession.createCriteria(FriendlinkType.class)
                .list();
        request.setAttribute("friendlinktypes", friendlinktypes);
        return view();
    }

    // 友情链接类型编辑
    @RequestMapping("edit")
    public String edit() {
        int friendlinktypeId = request.getParameter("friendlinktypeid") == null ? 0 :
                Integer.parseInt(request.getParameter("friendlinktypeid"));
        FriendlinkType friendlinktype = friendlinktypeId == 0 ? new FriendlinkType() :
                factorySession.get(FriendlinkType.class, friendlinktypeId);
        request.setAttribute("friendlinktype", friendlinktype);
        return view();
    }

    // 友情链接类型编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_plugin_frendlink");
        int friendlinktypeId = Integer.parseInt(request.getParameter("friendlinktypeid"));
        FriendlinkType friendlinktype = friendlinktypeId == 0 ? new FriendlinkType() :
                factorySession.load(FriendlinkType.class, friendlinktypeId, LockMode.PESSIMISTIC_WRITE);
        friendlinktype.setName(request.getParameter("name"));
        if (friendlinktypeId == 0) factorySession.save(friendlinktype);
        recordLog((friendlinktypeId == 0 ? "新建" : "更新") + "友情链接类型《" + friendlinktype.getName().trim() + "》");
        PageHelper.alertAndRedirect("保存成功", "./");
    }

    // 友情链接类型删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_plugin_frendlink");
        int friendlinktypeId = Integer.parseInt(request.getParameter("friendlinktypeid"));
        FriendlinkType friendlinktype = factorySession.load(FriendlinkType.class, friendlinktypeId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(friendlinktype);
        recordLog("删除友情链接类型《" + friendlinktype.getName().trim() + "》");
        PageHelper.alertAndRedirect("删除成功", "./");
    }

}
